﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClassLibrary;

namespace ObTookit
{
    public partial class FormConfig : Form
    {
        public FormConfig()
        {
            InitializeComponent();
        }

        private void FormConfig_Load(object sender, EventArgs e)
        {
            readAppConfig();
        }

        private void btn_conf_dbcs_Click(object sender, EventArgs e)
        {
            if(ConfigUtil.UpdateConnectionStrings("ObTookit.exe.config", "DBCS", tbx_conf_dbcs.Text))
            {
                MessageBox.Show("更新数据库连接字符串成功！");
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_conf_setapp_Click(object sender, EventArgs e)
        {
            ConfigUtil.SetAppSetting("DbAlias", tbx_dbAlias.Text);
            ConfigUtil.SetAppSetting("LgBaseCLS", tbx_lgBaseCls.Text);
            ConfigUtil.SetAppSetting("ServBaseCLS", tbx_ServBaseCls.Text);
            ConfigUtil.SetAppSetting("ApiBaseCLS", tbx_apiBaseCls.Text);

            ConfigUtil.SetAppSetting("ModelNS", tbx_modelNS.Text);
            ConfigUtil.SetAppSetting("QueryNS", tbx_queryNS.Text);
            ConfigUtil.SetAppSetting("PermitNS", tbx_permitNS.Text);

            ConfigUtil.SetAppSetting("DalNS", tbx_dalNS.Text);
            ConfigUtil.SetAppSetting("LgNS", tbx_lgNS.Text);

            ConfigUtil.SetAppSetting("BaseNS", tbx_baseNS.Text);
            ConfigUtil.SetAppSetting("BaseSession", tbx_baseSession.Text);

            ConfigUtil.SetAppSetting("ServerNS", tbx_serverNS.Text);
            ConfigUtil.SetAppSetting("ApiNS", tbx_apiNS.Text);

            ConfigUtil.SetAppSetting("CtrlNS", tbx_ctrlNS.Text);
            ConfigUtil.SetAppSetting("ViewNS", tbx_viewNS.Text);
        }

        private void readAppConfig()
        {
            readConnectionStrings();
            readAppSettings();
        }
        private void readConnectionStrings()
        {
            try
            {
                tbx_conf_dbcs.Text = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                tbx_conf_sqlite.Text = ConfigurationManager.ConnectionStrings["SQLiteConn"].ConnectionString;
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取ConnectionStrings异常：\r\n" 
                    + ex.Message + "\r\n" + ex.StackTrace);
            }
        }
        private void readAppSettings()
        {
            try
            {
                tbx_dbAlias.Text = ConfigurationManager.AppSettings["DbAlias"];
                tbx_lgBaseCls.Text = ConfigurationManager.AppSettings["LgBaseCLS"];
                tbx_ServBaseCls.Text = ConfigurationManager.AppSettings["ServBaseCLS"];
                tbx_apiBaseCls.Text = ConfigurationManager.AppSettings["ApiBaseCLS"];
                //
                tbx_modelNS.Text = ConfigurationManager.AppSettings["ModelNS"];
                tbx_queryNS.Text = ConfigurationManager.AppSettings["QueryNS"];
                tbx_permitNS.Text = ConfigurationManager.AppSettings["PermitNS"];
                //
                tbx_dalNS.Text = ConfigurationManager.AppSettings["DalNS"];
                tbx_lgNS.Text = ConfigurationManager.AppSettings["LgNS"];
                //
                this.tbx_baseNS.Text = ConfigurationManager.AppSettings["BaseNS"];
                this.tbx_baseSession.Text = ConfigurationManager.AppSettings["BaseSession"];
                //
                tbx_serverNS.Text = ConfigurationManager.AppSettings["ServerNS"];
                tbx_apiNS.Text = ConfigurationManager.AppSettings["ApiNS"];
                //
                tbx_ctrlNS.Text = ConfigurationManager.AppSettings["CtrlNS"];
                tbx_viewNS.Text = ConfigurationManager.AppSettings["ViewNS"];

            }
            catch (Exception ex)
            {
                MessageBox.Show("读取dAppSettings异常：\r\n"
                    + ex.Message + "\r\n" + ex.StackTrace);
            }
        }
    }
}
